package br.uniceub.fsw.academicmobile.control;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import br.uniceub.fsw.academicmobile.dao.AulaDAO;
import br.uniceub.fsw.academicmobile.dao.CursoDAO;
import br.uniceub.fsw.academicmobile.dao.DisciplinaDAO;
import br.uniceub.fsw.academicmobile.dao.FrequenciaDAO;
import br.uniceub.fsw.academicmobile.dao.HistoricoDAO;
import br.uniceub.fsw.academicmobile.dao.MencaoDAO;
import br.uniceub.fsw.academicmobile.dao.ProfessorDAO;
import br.uniceub.fsw.academicmobile.dao.TurmaDAO;
import br.uniceub.fsw.academicmobile.vo.AulaVO;
import br.uniceub.fsw.academicmobile.vo.CursoVO;
import br.uniceub.fsw.academicmobile.vo.DisciplinaVO;
import br.uniceub.fsw.academicmobile.vo.FrequenciaVO;
import br.uniceub.fsw.academicmobile.vo.HistoricoVO;
import br.uniceub.fsw.academicmobile.vo.MencaoVO;
import br.uniceub.fsw.academicmobile.vo.ProfessorVO;

/**
 * 
 * @author Pedro Ivo Povoa
 */
public class GenerateXML {
	private Document doc;
	private Element elem;
	private Element professor;
	private Element curso;
	private Element disciplina;
	private Element historico;
	private Element frequencia;
	private Element aluno;
	private Element alunos;
	private Element aula;
	private Element notaAula;
	private Element aulas ;
	private Element mencao;
	private Element mencoes;
	private Element ra;
	private ProfessorVO pVO;
	private List<HistoricoVO> lhistVO;
	private List<AulaVO> lAulaVO = new ArrayList();;
	private boolean contem = false;
        private ProfessorDAO daoProfessor;
        private CursoDAO daoCurso;
        private DisciplinaDAO daoDisciplina;
        private TurmaDAO daoTurma;
        private HistoricoDAO daoHistorico;
        private AulaDAO daoAula;
        private FrequenciaDAO daoFrequencia;
        private MencaoDAO daoMencao;

	public GenerateXML(HttpServletRequest request) throws Exception {
                daoProfessor = new ProfessorDAO();
                daoAula = new AulaDAO();
                daoCurso = new CursoDAO();
                daoMencao = new MencaoDAO();
                daoFrequencia = new FrequenciaDAO();
                daoTurma = new TurmaDAO();
                daoHistorico = new HistoricoDAO();
                daoDisciplina = new DisciplinaDAO();
		System.out.println("Entrou no gerador");
                pVO = new ProfessorVO();
                pVO.setIdCdDrt(new Long(request.getParameter("drt")));
		pVO = daoProfessor.consultarProfessorPorId(pVO);
		professor = createElem("professor");
		doc = createDoc(professor);
		addAtribute(professor, "drt", Long.toString(pVO.getIdCdDrt()));
		addAtribute(professor, "nome", pVO.getNoProfessor());
		System.out.println("criou element professor");

		for (CursoVO cVO : daoCurso.getCursoForProfessor(pVO)) {
			curso = createElem("curso");
			addAtribute(curso, "id", Long.toString(cVO.getIdCdCurso()));
			addAtribute(curso, "nome", cVO.getNoCurso());
			System.out.println("criou element curso");

			for (DisciplinaVO dVO : daoDisciplina.getDisciplinaByTurma(
							daoTurma.getTurmaForProfessor(pVO))) {
				disciplina = createElem("disciplina");
				addAtribute(disciplina, "id", Long.toString(dVO.getIdCdDisciplina()));
				addAtribute(disciplina, "nome", dVO.getNoDisciplina());
				addAtribute(disciplina, "turma", daoTurma.getTurmaByDisciplina(dVO.getIdCdDisciplina()).getSgTurma());
				addAtribute(disciplina, "periodo", dVO.getNrPeriodo().toString());
				System.out.println("criou element disciplina");
				aulas = createElem("aulas");
				historico = createElem("historico");
				alunos = createElem("alunos");
				lhistVO = daoHistorico.getHistoricoForDisciplina(
						dVO);
				for (AulaVO aVO : daoAula.getAulaByTurma(
						daoTurma.getTurmaByDisciplina(dVO.getIdCdDisciplina()))) {
					aula = createElem("aula");
					addAtribute(aula, "data", "" + aVO.getDtAula());
					notaAula = createElem("nota");
					addText(notaAula, aVO.getTeAula());
					addElement(aula, notaAula);
					frequencia = createElem("frequencia");
					aula.detach();
					addElement(aulas, aula);
					for (HistoricoVO hVO : lhistVO) {
					for (FrequenciaVO fVO : daoFrequencia.getFrequenciaByAula(aVO, hVO)) {

						ra = createElem("ra");
						addAtribute(ra, "id", "" + hVO.getAluno().getIdCdRa());
						addAtribute(ra, "p1", "" + fVO.getTpFrequencia1());
						addAtribute(ra, "p2", "" + fVO.getTpFrequencia2());
						addElement(frequencia, ra);
						frequencia.detach();
						addElement(aula, frequencia);
					}
				    }
				}
					for (HistoricoVO hVO : lhistVO) {
						
						aluno = createElem("aluno");
						addAtribute(aluno, "ra", Long.toString(hVO.getAluno()
								.getIdCdRa()));
						addAtribute(aluno, "nome", hVO.getAluno().getNoAluno());
						addAtribute(aluno, "totalPresenca", Integer
								.toString(hVO.getQtFrequencia()));
						addAtribute(aluno, "mencaoFinal", ""+hVO.getSgMencao());
                        mencoes = createElem("mencoes");
                        for(MencaoVO mVO : daoMencao.getMencaoByHistorico(hVO)){
                        	mencao = createElem("mencao");
                        	addAtribute(mencao, "data", ""+mVO.getDtMencao());
                        	addAtribute(mencao, "tipo", mVO.getTipoMencao().getNoTipoMencao());
                        	addText(mencao, mVO.getSgMencao().getSgMencao());
                        	addElement(mencoes, mencao);
                        }
                        addElement(aluno, mencoes);
						addElement(alunos, aluno);

					}
				

				aulas.detach();
				addElement(disciplina, aulas);
				addElement(historico, alunos);

				addElement(disciplina, historico);

				addElement(curso, disciplina);
			}

			addElement(professor, curso);
		}
		XMLOutputter xout = new XMLOutputter();
        System.out.println("criou XML");
		try {
          System.out.println("ira externalizar arquivo");
 			// Criando o arquivo de saida
            File file = new File(request.getRealPath("/")+"/WEB-INF/temp/"+request.getParameter("drt")+".xml");
            System.out.println("criou Fil�");
			FileWriter arquivo = new FileWriter(file);
			System.out.println("criou file writer");
            System.out.println(file.toString());
			// Imprimindo o XML no arquivo
            
			xout.output(doc, arquivo);

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

	private Document createDoc(Element name) {
		doc = new Document(name);
		return doc;
	}

	private Element createElem(String name) {
		elem = new Element(name);
		return elem;
	}

	private void addAtribute(Element elem, String nome, String valor) {
		elem.setAttribute(nome, valor);
	}

	private void addText(Element elem, String corpo) {
		elem.setText(corpo);
	}

	private void addElement(Element parent, Element child) {
		parent.addContent(child);
	}

}
